#include "bsp.h"

t_uartRbuf t_leuartBuf[LEUART_MAX_LEN];       //定义一个串口一接收数据的缓冲区
t_uartRec _uartReceive = {0,0};    //初始化读写索引为0

unsigned char nbSetState = 0; //状态机 初始化为0
extern t_uartRec _uartReceive;

/**************************************************************************************************
 * 缓冲区当前所有数据长度
 *************************************************************************************************/
unsigned char buffSize(void)
{
    short  wi = _uartReceive.writeIndex;
    short  ri = _uartReceive.readIndex;
//	printf(wi);
//	printf(ri);
    return (wi >= ri) ? (wi - ri) : (LEUART_MAX_LEN - ri + wi);
}

/**************************************************************************************************
 * 缓冲区是否空了
 *************************************************************************************************/
unsigned char isEmpty(void)
{
    short  wi = _uartReceive.writeIndex;
    short  ri = _uartReceive.readIndex;
    return (wi == ri);
}

/**************************************************************************************************
 * 缓冲区是否满了
 *************************************************************************************************/
unsigned char isFull(void)
{
    short  wi = _uartReceive.writeIndex;
    short  ri = _uartReceive.readIndex;
    return ((wi + 1) % LEUART_MAX_LEN == ri);
}

/**************************************************************************************************
 * 将串口接收到的 数据 从顶部压入堆栈
 *************************************************************************************************/
void pushBuff (t_uartRbuf data)
{
    if (isFull())
    {
      return;
    }
    memcpy(t_leuartBuf[_uartReceive.writeIndex].rbuf,data.rbuf,data.len);          //rbuf 数值传递
    t_leuartBuf[_uartReceive.writeIndex].len = data.len;                           //len数值传递
    
	//printf(_uartReceive.buff[_uartReceive.writeIndex]);
    _uartReceive.writeIndex = (_uartReceive.writeIndex + 1) % LEUART_MAX_LEN;
}  

/**************************************************************************************************
 * 将堆栈里面的数据从底部弹出
 *************************************************************************************************/

t_uartRbuf popBuff(void)
{
  t_uartRbuf d;
  memcpy(d.rbuf,t_leuartBuf[_uartReceive.readIndex].rbuf,t_leuartBuf[_uartReceive.readIndex].len);
	d.len = t_leuartBuf[_uartReceive.readIndex].len;
	_uartReceive.readIndex = (_uartReceive.readIndex + 1) % LEUART_MAX_LEN;    
	return d;  
}

